package com.monkey.luffer.springboot.demo.basis.designPatterns.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: dengkaibing
 * @Description: CGLIB代理
 * @Date: 2021/2/3 10:28
 * @company 甜橙金融
 * @Version: 1.0
 */
public class CglibProxy implements MethodInterceptor {
    Object target;
    public Object getInstance(){
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(this.target.getClass());
        //设置回调函数
        enhancer.setCallback(this);
        return enhancer.create();
    }
    public CglibProxy(Object target){
        this.target = target;
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("买房前准备！");
        //目标对象干的事情
        Object result=methodProxy.invoke(target,objects);
        System.out.println("买房后动作！");
        return result;
    }
}
